//
//  WmcHomeTopMenuView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/3.
//

import UIKit

public extension WmcButtonItemType {
    /// 按钮类型
    enum HomeTopMenu {
        /// 画幅
        case aspectRatio

        /// 清晰度
        case articulation

        /// 延时
        case delay

        /// 联系我们
        case contact

        /// 云相册
        case cameraSetting

        /// 自定义
        case custom(view: UIView)
    }
}

// MARK: - [WmcHomeTopMenuView]

class WmcHomeTopMenuView: WmcBaseView {
    lazy var stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 5
        return stackView
    }()

    /// 画幅
    lazy var aspectRatioBtn: WmcAspectRatioBtn = {
        let btn = WmcAspectRatioBtn.Create()
        return btn
    }()

    /// 清晰度
    lazy var articulationBtn: WmcArticulationBtn = {
        let btn = WmcArticulationBtn.Create()
        return btn
    }()

    /// 延迟
    lazy var delayBtn: WmcDelayBtn = .Create()

    /// 联系我们
    lazy var contactBtn: WmcTopImgBottomTextBtn = {
        let btn = WmcTopImgBottomTextBtn.CreateHomeHeaderBtn(title: "联系我们", image: "home_menu_contact_us".wmc_image)
        return btn
    }()

    /// 相机设置
    lazy var cameraSettingBtn: WmcTopImgBottomTextBtn = {
        let btn = WmcTopImgBottomTextBtn.CreateHomeHeaderBtn(title: "相机设置", image: "home_menu_cam_setting".wmc_image)
        return btn
    }()

    /// 功能模式
    var functionMode: WmcButtonItemType.HomeFunctionMode = .photo

    var homeDelegate: WmcHomeDelegate? {
        return YPWmcManager.shared.uiDelegate.homeDelegate
    }

    override func config() {
        // 刷新按钮
        YPWmcDrivingManager.shared.home_reloadTopMenuItemsBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.reloadItems()
        }
    }

    override func makeUI() {
        backgroundColor = .black.yp.alpha(0.7)
        layer.cornerRadius = 5
        layer.masksToBounds = true

        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(16)
            make.bottom.equalTo(-12)
            make.right.equalTo(-16)
        }
    }

    /// 切换模式
    func switchMode(_ mode: WmcButtonItemType.HomeFunctionMode) {
        functionMode = mode
        reloadItems()
    }

    /// 刷新按钮
    func reloadItems() {
        stackView.yp.arrangedSubviews = btns()
    }

    /// 按钮
    func btns() -> [UIView] {
        var defaultCase: [WmcButtonItemType.HomeTopMenu] = []
        switch functionMode {
        case .photo: // 拍照
            defaultCase = [
                .aspectRatio, .delay, .contact, .cameraSetting,
            ]
        case .video: // 视频
            defaultCase = [
                .articulation, .contact, .cameraSetting,
            ]
        default: break
        }
        if let caseList = homeDelegate?.topMenuList(.current, functionMode: functionMode, defaultList: defaultCase) {
            defaultCase = caseList
        }
        let btns: [UIView] = toItemList(caseList: defaultCase)
        return btns
    }

    /// 转换成按钮实体
    func toItemList(caseList: [WmcButtonItemType.HomeTopMenu]) -> [UIView] {
        var items: [UIView] = []
        caseList.forEach { cas in
            switch cas {
            case .aspectRatio:
                items.append(aspectRatioBtn)
            case .articulation:
                items.append(articulationBtn)
            case .delay:
                items.append(delayBtn)
            case .contact:
                items.append(contactBtn)
            case .cameraSetting:
                items.append(cameraSettingBtn)
            case let .custom(view: view):
                items.append(view)
            }
        }
        return items
    }
}
